<template>
  <div :class="['history-panel', { open: isOpen }]">
    <div class="panel-header">
      <h3>对话历史</h3>
      <button class="close-btn" @click="$emit('close')">
        <i class="fas fa-times"></i>
      </button>
    </div>
    
    <div class="panel-content">
      <button class="new-chat-btn" @click="$emit('new-chat')">
        <i class="fas fa-plus"></i>
        新建对话
      </button>
      
      <div class="history-list">
        <div 
          v-for="chat in chatHistory" 
          :key="chat.id"
          :class="['history-item', { active: currentChatId === chat.id }]"
          @click="$emit('load-chat', chat.id)"
        >
          <div class="item-icon">
            <i class="fas fa-comment"></i>
          </div>
          <div class="item-content">
            <div class="item-title">{{ chat.title }}</div>
            <div class="item-preview">
              {{ getLastMessage(chat) }}
            </div>
            <div class="item-time">
              {{ formatTime(chat.messages[0]?.timestamp) }}
            </div>
          </div>
          <button class="delete-btn" @click.stop="deleteChat(chat.id)">
            <i class="fas fa-trash"></i>
          </button>
        </div>
        
        <div v-if="chatHistory.length === 0" class="empty-state">
          <i class="fas fa-comment-slash"></i>
          <p>暂无对话历史</p>
          <span>开始新的对话吧！</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HistoryPanel',
  props: {
    chatHistory: {
      type: Array,
      default: () => []
    },
    currentChatId: {
      type: Number,
      default: null
    },
    isOpen: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    getLastMessage(chat) {
      const lastMessage = chat.messages[chat.messages.length - 1];
      if (!lastMessage) return '暂无消息';
      
      const content = lastMessage.content;
      return content.length > 30 ? content.substring(0, 30) + '...' : content;
    },
    
    formatTime(timestamp) {
      if (!timestamp) return '';
      
      const date = new Date(timestamp);
      const now = new Date();
      const diff = now - date;
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));
      
      if (days === 0) {
        return date.toLocaleTimeString('zh-CN', {
          hour: '2-digit',
          minute: '2-digit'
        });
      } else if (days === 1) {
        return '昨天';
      } else if (days < 7) {
        return `${days}天前`;
      } else {
        return date.toLocaleDateString('zh-CN');
      }
    },
    
    deleteChat(chatId) {
      if (confirm('确定要删除这个对话吗？')) {
        const updatedHistory = this.chatHistory.filter(chat => chat.id !== chatId);
        localStorage.setItem('chatHistory', JSON.stringify(updatedHistory));
        this.$emit('chat-deleted', chatId);
      }
    }
  }
}
</script>

<style scoped>
.history-panel {
  width: 350px;
  background: rgba(30, 41, 59, 0.95);
  backdrop-filter: blur(10px);
  border-right: 1px solid #334155;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
}

.panel-header {
  padding: 20px;
  border-bottom: 1px solid #334155;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(15, 23, 42, 0.8);
}

.panel-header h3 {
  color: #e2e8f0;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s;
}

.close-btn:hover {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
}

.panel-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.new-chat-btn {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  border: none;
  color: white;
  padding: 12px 20px;
  margin: 20px;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
}

.new-chat-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(59, 130, 246, 0.3);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 20px 20px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 8px;
  position: relative;
}

.history-item:hover {
  background: rgba(96, 165, 250, 0.1);
}

.history-item.active {
  background: rgba(96, 165, 250, 0.2);
  border: 1px solid rgba(96, 165, 250, 0.3);
}

.item-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(96, 165, 250, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #60a5fa;
  flex-shrink: 0;
}

.item-content {
  flex: 1;
  min-width: 0;
}

.item-title {
  color: #e2e8f0;
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-preview {
  color: #94a3b8;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}

.item-time {
  color: #64748b;
  font-size: 11px;
}

.delete-btn {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 6px;
  border-radius: 4px;
  opacity: 0;
  transition: all 0.3s;
}

.history-item:hover .delete-btn {
  opacity: 1;
}

.delete-btn:hover {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #94a3b8;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 16px;
  margin-bottom: 8px;
  color: #cbd5e1;
}

.empty-state span {
  font-size: 14px;
}

@media (max-width: 768px) {
  .history-panel {
    position: absolute;
    top: 60px;
    left: 0;
    bottom: 0;
    z-index: 1000;
    transform: translateX(-100%);
  }
  
  .history-panel.open {
    transform: translateX(0);
  }
  
  .panel-header {
    padding: 16px;
  }
  
  .new-chat-btn {
    margin: 16px;
    padding: 10px 16px;
  }
  
  .history-list {
    padding: 0 16px 16px;
  }
}

@media (max-width: 480px) {
  .history-panel {
    width: 100%;
  }
}
</style>